<?php use_helper('Javascript','Form')?>
      <table style="border: thin; border-style: solid;">
        <tr>
          <td style="vertical-align: top;">
            <?php if(! $invoiceForm->isNew()):?>
              <?php if($invoiceAmountsForm->isNew()):?>
                <?php
                  echo
                    form_remote_tag(
                     array(
                       'url'=>'invoice/insertInvoiceAmounts',
                       'update'=>'divInvoiceAmountsForm',
                       'complete'=>"Element.hide('indicator');".
                          remote_function(
                            array(
                              'update'=>'row_invoice_'.$invoiceForm->getObject()->getInvoiceIden(),
                              'url'=>'invoice/getTd?invoice_iden='.$invoiceForm->getObject()->getInvoiceIden(),
                            )
                          ),
                       'script'=>true,
                       'loading'=>"Element.show('indicator')",
                     )
                   )
                ?>
              <?php else:?>
                <?php
                  echo
                    form_remote_tag(
                     array(
                       'url'=>'invoice/updateInvoiceAmounts',
                       'update'=>'divInvoiceAmountsForm',
                       'complete'=>"Element.hide('indicator');",
                       'script'=>true,
                       'loading'=>"Element.show('indicator')",
                     )
                   )
                ?>
              <?php endif;?>
            <?php endif;?>
            <?php 
              $invoice_iden = 
                $invoiceAmountsForm->isNew()?$invoiceForm->getObject()->getInvoiceIden():$invoiceAmountsForm->getObject()->getInvoiceIden();
              echo input_hidden_tag('invoice_amounts[invoice_iden]',$invoice_iden);
            ?>
            <?php echo $invoiceAmountsForm['amount_iden']?>
            <table>
              <thead>
                <tr>
                  <th class="field"><?php echo $invoiceAmountsForm['description']->renderLabel()?></th>
                  <td class="field">
                    <?php echo $invoiceAmountsForm['description']->renderError()?>
                    <?php echo $invoiceAmountsForm['description']?>
                  </td>
                </tr>
                <tr>
                  <th class="field"><?php echo $invoiceAmountsForm['amount']->renderLabel()?></th>
                  <td class="field">
                    <?php echo $invoiceAmountsForm['amount']->renderError()?>
                    <?php echo $invoiceAmountsForm['amount']?>
                  </td>
                </tr>
                <tr>
                  <th class="field"> Discounts </th>
                  <td class="field">
                    <?php echo $invoiceAmountsForm['discount1']->renderLabel()?>
                    <?php echo $invoiceAmountsForm['discount1']->renderError()?>
                    <?php echo $invoiceAmountsForm['discount1']?>
                    <?php echo $invoiceAmountsForm['discount2']->renderLabel()?>
                    <?php echo $invoiceAmountsForm['discount2']->renderError()?>
                    <?php echo $invoiceAmountsForm['discount2']?>
                    <?php echo $invoiceAmountsForm['discount3']->renderLabel()?>
                    <?php echo $invoiceAmountsForm['discount3']->renderError()?>
                    <?php echo $invoiceAmountsForm['discount3']?>
                    <?php echo $invoiceAmountsForm['discount4']->renderLabel()?>
                    <?php echo $invoiceAmountsForm['discount4']->renderError()?>
                    <?php echo $invoiceAmountsForm['discount4']?>
                  </td>
                </tr>
              </thead>
            </table>
            <?php if(! $invoiceForm->isNew()):?>
                <div class="form_buttons">
                  <?php echo submit_tag('Save');?> |
                  <?php if((! $invoiceAmountsForm->isNew())&&($invoiceForm->getObject()->countInvoiceAmountss()>1)):?>
                    <?php 
                      echo 
                        button_to_remote(
                          'Delete',
                            array(
                             'url'=>'invoice/deleteInvoiceAmounts?invoice_iden='.$invoiceAmountsForm->getObject()->getInvoiceIden().
                               '&amount_iden='.$invoiceAmountsForm->getObject()->getAmountIden(),
                             'update'=>'divInvoiceAmountsForm',
                             'script'=>true,
                             'complete'=>"Element.hide('indicator');".
                               remote_function(
                                 array(
                                   'update'=>'row_invoice_'.$invoiceForm->getObject()->getInvoiceIden(),
                                   'url'=>'invoice/getTd?invoice_iden='.$invoiceForm->getObject()->getInvoiceIden(),
                                 )
                               ),
                             'loading'=>"Element.show('indicator');",
                            )
                        );
                    ?> |
                  <?php endif;?>
                  <?php 
                    echo 
                      button_to_remote(
                        'Reset',
                          array(
                           'url'=>'invoice/createInvoiceAmounts?invoice_iden='.$invoiceForm->getObject()->getInvoiceIden(),
                           'update'=>'divInvoiceAmountsForm',
                           'script'=>true,
                          )
                      );
                  ?>
                </div>
                </form>
            <?php endif;?>
          </td> 
          <td style="vertical-align: top;">
            <table class="datagrid">
              <thead class="datagrid">
                <tr>
                  <th>Description</th>
                  <th>Amount</th>
                  <th>Discounts</th>
                  <th>Subtotal</th>
                  <th style="width:16px;"></th>
                </tr>
              </thead>
              <tbody class="datagrid" style="height:150px;">
                <?php $total['subtotal'] = 0;?>
                <?php foreach($invoice_amountss as $invoice_amounts):?>
                <tr>
                  <td>
                    <?php 
                      echo 
                        link_to_remote(
                          $invoice_amounts->getDescription(),
                          array(
                            'url'=>'invoice/editInvoiceAmounts?invoice_iden='.
                              $invoice_amounts->getInvoiceIden().'&amount_iden='.$invoice_amounts->getAmountIden(),
                            'update'=>'divInvoiceAmountsForm',
                            'script'=>true,
                          )
                        );
                    ?>
                  </td>
                  <td style="text-align:right;"><?php echo number_format($invoice_amounts->getAmount(),2);?></td>
                  <td>
                    <?php echo $invoice_amounts->getDiscount1();?>%
                    <?php echo $invoice_amounts->getDiscount2();?>%
                    <?php echo $invoice_amounts->getDiscount3();?>%
                    <?php echo $invoice_amounts->getDiscount4();?>%
                  </td>
                  <td style="text-align:right;">
                    <?php echo number_format($invoice_amounts->getSubtotal(),2);?>
                  </td>
                  <td></td>
                </tr>
                <?php $total['subtotal'] += $invoice_amounts->getSubtotal();?>
                <?php endforeach;?>
              </tbody>
              <tfoot class="datagrid">
                <tr>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td style="text-align:right;"><?php echo number_format($total['subtotal'],2);?></td>
                  <td></td>
                  <td></td>
                </tr>
              </tfoot>
            </table>
          </td>
        </tr>
      </table>